﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IServices;
using Services.Validation;
using IRepositories;
using LinqRepositories;
using GestionLinqLibData;



namespace Services
{
    public class TypeExamService : ITypeExamService
    {
        private IValidationDictionary _validationDictionary;
        private ITypeExamRepository _repository;

        public TypeExamService(IValidationDictionary validationDictionary, ITypeExamRepository repository)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
        }

        public TypeExamService(IValidationDictionary validationDictionary)
            : this(validationDictionary, new LinqTypeExamRepository())
        { }

        protected bool ValidateType(type_exam typeToValidate)
        {
            if (typeToValidate.nom_exam.Trim().Length == 0)
                _validationDictionary.AddError("nom de type vide", "nom de type null"); 

            return _validationDictionary.IsValid;

        }

        #region ITypeExamService Membres

        public bool CreateType(type_exam typeToCreate)
        {
            if (!ValidateType(typeToCreate))
                return false;
            try
            {
                _repository.CreateExam(typeToCreate);
            }
            catch
            {

                return false;
            }
            return true;

        }

        public type_exam GetType(int id_type)
        {
            return _repository.GetType(id_type);
        }

        public IEnumerable<type_exam> ListTypes()
        {
            return _repository.ListTypes();
        }

        public bool EditType(type_exam typeToEdit)
        {
            if (!ValidateType(typeToEdit))
                return false;

            try
            {
                _repository.EditType(typeToEdit);

            }
            catch
            {

                return false;
            }
            return true;

        }

        public bool DeleteType(type_exam typeToDelete)
        {
            try
            {
                _repository.DeleteType(typeToDelete);
            }
            catch
            {

                return false;
            }

            return true;

        }

        #endregion
    }
}